package bounjour;

import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

/**
 * @author Senem Beken
 * @date: 02.05.11
 */

public class User 
{	
	private String userName;
	// Actual name of the user, not user name!
	private String name;
	private String eMail;
	private Date registerDate;
	private String password;
	
	private static RecordManager recordManager = new RecordManager();
	
	public User(String userName, String name, String eMail, Date registerDate)
	{	
		this.setUserName(userName);
		this.setName(name);
		this.seteMail(eMail);
		this.setRegisterDate(registerDate);
		this.setPassword(password);
	}

	// Interface functions
	static public boolean login(String username, String password) 
	{
		return true;
	}
	
	static public boolean register(String username, String password, String name, String email) throws Exception 
	{
		String tableName = "Users";
		
		// check if user name is already present in the Users table
		@SuppressWarnings("rawtypes")
		Vector resultSet = recordManager.selectSingleRow("Username", tableName, username);
		if(resultSet.size() > 0)
		{
			System.out.println("User name already taken");
			return false;
		}
		
		// validate e-mail
		Util.validateEmail(email);
		
		// all checks passed. insert user into the Users table 
		Date registerDate = new Date();
		
		ArrayList <String> valueList = new ArrayList<String>();
		valueList.add(name);
		valueList.add(email);
		valueList.add(password);
		valueList.add(registerDate.toString());
		
		ArrayList <String> columnList = new ArrayList<String>();
		columnList.add("Name");
		columnList.add("Email");
		columnList.add("Password");
		columnList.add("ResgisterDate");
		
		int insertionResult = recordManager.insertSingleRecord(valueList, tableName, columnList);
		if(insertionResult > 0)
		{
			System.out.println("User created successfully");
			return true;
		}
		else
		{
			System.out.println("Error while creating the user");
			return false;
		}
	}
	
	// TODO
	static public User getCurrentUser()
	{
		return null;
	}
	
	// TODO 
	static public boolean isLogin()
	{
		return getCurrentUser() != null;
	}
	
	// TODO
	public ArrayList<User> getFollowers() 
	{
		return null;
	}
	
	// TODO
	public ArrayList<User> getFollowedUsers() 
	{
		return null;
	} 
	
	public String getName()
	{
		return name;
	}
	
	public String geteMail()
	{
		return eMail;
	}
	
	public Date getRegisterDate() 
	{ 
		return registerDate; 
	}

	public String getUserName() 
	{
		return userName;
	}
	
	public void setUserName(String userName) 
	{
		this.userName = userName;
	}

	public void seteMail(String eMail)
	{
		Util.validateEmail(eMail);
		this.eMail = eMail;
	}

	public void setRegisterDate(Date registerDate) 
	{
		this.registerDate = registerDate;
	}

	public void setName(String name) 
	{
		this.name = name;
	}
	
	private void setPassword(String password) 
	{
		this.password = password;
	}
}
